package com.weaver.seconddev.tl.e9apis;

import java.math.BigDecimal;

import com.weaver.common.i18n.tool.util.I18nContextUtil;
import com.weaver.framework.rpc.context.impl.TenantRpcContext;
import com.weaver.verupgrade.conn.RecordSet;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;

@RestController
@RequestMapping("/api/secondev/tl/api4wf")
@Slf4j
public class api4wf {


    // 发布守后，完整接口访问地址是否是：/api/secondev/tl/api4wf/checkzpqj

    // @PostMapping("/checkSphm")
    @PostMapping("/checkSphm")
    public int checkSphm(HttpServletRequest request, HttpServletResponse response){

        TenantRpcContext.setTargetTenantKey("tl2k2r96ty");  // 通力组织机构编码：tl2k2r96ty
        TenantRpcContext.setTargetEmployeeId("1089497602536882176");

        RecordSet rs= I18nContextUtil.getBean(RecordSet.class);
        String sphmwz =request.getParameter( "sphmwz");
        String sphm = request.getParameter("sphm");
        String zpqj = request.getParameter("zpqj");

        log.debug("===param sphm=="+sphm+"===zpqj==="+zpqj);

        int counts = 0;

        try {
            String sql = "select sphmhbw,zpqj from uf_spxxhzbcwzy  where sphmhbw='"+sphm+"' and zpqj='"+zpqj+"' limit 1";
            log.debug("=======商票财务台账sql====="+sql);
            rs.execute(sql);
            if (rs.next()){

                counts = 1;

                String zpqjValue = rs.getString("zpqj");

                log.debug("===zpqjValue =="+zpqjValue);


                if(!"".equals(zpqjValue) && !zpqj.equals(zpqjValue)){
                    //Mason:db.子票区间有值and不等于表单子票区间 则允许通过
                    counts = 0;
                }else if(!"".equals(zpqjValue) && zpqj.equals(zpqjValue)){
                    counts = 1;
                }else if("".equals(zpqjValue) && !zpqj.equals("")){
                    //Mason: db.子票区间无值and 表单子票有值 则允许通过
                    counts = 0;
                }else if("".equals(zpqjValue) && zpqj.equals("")){
                    counts = 1;
                }

            }

        }catch (Exception e) {
            log.error("承兑汇票事前申请表",e);
        }
        return counts;
    }

    @PostMapping("/checkzpqj")
    public int checkzpqj(HttpServletRequest request, HttpServletResponse response){

        TenantRpcContext.setTargetTenantKey("tl2k2r96ty");  // 通力组织机构编码：tl2k2r96ty
        TenantRpcContext.setTargetEmployeeId("1089497602536882176");

        RecordSet rs= I18nContextUtil.getBean(RecordSet.class);
        String sphmwz =request.getParameter( "sphmwz");

        int counts = 99;
        try {
            String sql = "select count(zpqj) from uf_qxspqdsjnh where pjhm='"+sphmwz+"'";
            log.debug("=======checkzpqj sql====="+sql);
            rs.execute(sql);
            if (rs.next()){
                counts = Integer.parseInt(rs.getString(String.valueOf(1)));//帮我修改
            }
        }catch (Exception e) {
            log.error("承兑汇票事前申请表",e);
        }
        return counts;
    }


    @PostMapping("/checkMoney")
    public int checkMoney(HttpServletRequest request, HttpServletResponse response){

        TenantRpcContext.setTargetTenantKey("tl2k2r96ty");  // 通力组织机构编码：tl2k2r96ty
        TenantRpcContext.setTargetEmployeeId("1089497602536882176");

        RecordSet rs= I18nContextUtil.getBean(RecordSet.class);
        String sphmwz =request.getParameter( "sphmwz");


        String tablename = request.getParameter("tablename");
        String spzje = request.getParameter("spzje");
        String ypspje = request.getParameter("ypspje");//合同金额
        int counts = 0;
        String jefw = "";
        try {
            log.debug("=======合同金额=====" + ypspje + "===spzje:" + spzje + "=");
            String sql = "select jefw from uf_sqsqjefwpz limit 1";
            log.debug("=====查询金额范围sql=====" + sql);
            rs.execute(sql);
            if (rs.next()) {
                jefw = rs.getString("jefw");
            }

            BigDecimal bd1 = BigDecimal.valueOf(Double.parseDouble(ypspje));
            BigDecimal bd2 = BigDecimal.valueOf(Double.parseDouble(jefw));
            BigDecimal bd3 = BigDecimal.valueOf(Double.parseDouble(spzje));

            int r0 = bd3.compareTo(bd1);
            if (r0 > 0) {
                //减法
                BigDecimal bignum3 = bd3.subtract(bd1);
                // 商品总金额-合同金额＞范围值 (范围外)
                int r1 = bignum3.compareTo(bd2);
                if (r1 > 0) {
                    counts = 1;
                }
            } else {
                counts = 2;
            }
        } catch (Exception e) {
            log.error("承兑汇票事前申请表", e);
        }

        return counts;
    }


}
